package process;

import gui.Gui;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import model.SentimentModel;

import Comment.Comment;

public class Run {
	static private String specialCasePath = "data/special.txt";
	static private String adjPath = "";
	static private String verbPath = "";
	static private String entityPath = "";
	static private String propertyPath = "";
	static private String negativePath = "";
	static private String startEntiTag = "<enti>";
	static private String endEntiTag = "</enti>";
	static private String startAdjTag = "<adj>";
	static private String endAdjTag = "</adj>";
	
	static public   String oneEntityCommentsPath = "E:/research/projectCoding/VietnameseSentimentOfComment/input/oneEntityComments.txt";
	static public   String multEntityCommentPath = "E:/research/projectCoding/VietnameseSentimentOfComment/input/multEntityComments.txt";
	static public   String commentsPath = "E:/research/projectCoding/VietnameseSentimentOfComment/input/comments.txt";
	static public   String resultForOneEntityCommentsPath = "result/resultForOneEntityComments.txt";
	static public   String resultForMultEntityCommentsPath = "result/resultForMultEntityCommets.txt";
	static public   String resultForCommentsPath = "result/resultForAllComments.txt";
	static public   String positiveOrNegativeResultForOneEntityCommentsPath = "positiveOrNegativeResult/resultForOneEntityComments.txt";
	static public   String positiveOrNegativeResultForMultEntityCommentsPath = "positiveOrNegativeResult/resultForMultEntityCommets.txt";
	static public   String PositiveOrNegativeResultForCommentsPath = "positiveOrNegativeResult/resultForAllComments.txt";
	static public float p = 100;
	static public float r = 100;
	
	static public String mistakeFilePath = "mistake/mistake.txt";
	static public String negMistakeFilePath = "mistake/negativeMistake.txt";
	static public String positiveMistakeFilePath = "mistake/positiveMistake.txt";
	static public String neutralMistakeFilePath = "mistake/posnegMistake.txt";
	static public String realResult = "input/plain.txt";
	static public String mutlEntitiesMistakeComments = "mistake/multEntitiesMistakeComments.txt";
	public static int realPos = 0, realNeg = 0, realPN = 0, sysPos = 0, sysNeg = 0, sysPN = 0;
	
	public Run() {
		// TODO Auto-generated constructor stub
		setSpecialCasePath("data/specialEntityAdj.txt");
	}
	public static void setSpecialCasePath(String specialCasePath) {
		Run.specialCasePath = specialCasePath;
	}
	public static String getSpecialCasePath() {
		return specialCasePath;
	}
	public static void setStartEntiTag(String startEntiTag) {
		Run.startEntiTag = startEntiTag;
	}
	public static String getStartEntiTag() {
		return startEntiTag;
	}
	public static void setEndEntiTag(String endEntiTag) {
		Run.endEntiTag = endEntiTag;
	}
	public static String getEndEntiTag() {
		return endEntiTag;
	}
	
	public static void setNegativePath(String negativePath) {
		Run.negativePath = negativePath;
	}
	public static String getNegativePath() {
		return negativePath;
	}
	public static void setPropertyPath(String propertyPath) {
		Run.propertyPath = propertyPath;
	}
	public static String getPropertyPath() {
		return propertyPath;
	}
	public static void setEntityPath(String entityPath) {
		Run.entityPath = entityPath;
	}
	public static String getEntityPath() {
		return entityPath;
	}
	public static void setVerbPath(String verbPath) {
		Run.verbPath = verbPath;
	}
	public static String getVerbPath() {
		return verbPath;
	}
	public static void setAdjPath(String adjPath) {
		Run.adjPath = adjPath;
	}
	public static String getAdjPath() {
		return adjPath;
	}
	public static void setStartAdjTag(String startAdjTag) {
		Run.startAdjTag = startAdjTag;
	}
	
	public static void setEndAdjTag(String endAdjTag) {
		Run.endAdjTag = endAdjTag;
	}
	public static String getEndAdjTag() {
		return endAdjTag;
	}
	public static String getStartAdjTag() {
		// TODO Auto-generated method stub
		return startAdjTag;
	}
	
	public static long processForCommentsFile(String inputFilePath, String outputFilePath, String outputFilePath1){
		long time = System.currentTimeMillis();
		try {
			FileReader fr = new FileReader(inputFilePath);
			BufferedReader br = new BufferedReader(fr);
			String line = null;
			String result = "";
			String result1 = "";
			BufferedWriter out = new BufferedWriter(new FileWriter(Run.resultForOneEntityCommentsPath));
			BufferedWriter out1 = new BufferedWriter(new FileWriter(outputFilePath1));
			BufferedWriter out2 = new BufferedWriter(new FileWriter(Run.mistakeFilePath));
			BufferedWriter out3 = new BufferedWriter(new FileWriter(Run.positiveMistakeFilePath));
			BufferedWriter out4 = new BufferedWriter(new FileWriter(Run.negMistakeFilePath));
			BufferedWriter out5 = new BufferedWriter(new FileWriter(Run.neutralMistakeFilePath));
			BufferedWriter out6 = new BufferedWriter(new FileWriter(Run.mutlEntitiesMistakeComments));
			BufferedReader br1 = new BufferedReader(new FileReader(realResult));
			int lineNumber = 0;
			
			int numberOfMultiEnityComment = 0;
			int numberOfMultiEntityMistakeComments = 0;
			while((line = br.readLine())!=null){
				lineNumber++;
				Comment comment = new Comment(line);
				if(comment.getResult().getOneOrMultiEntity() == 1){
					result = Float.toString(comment.getResult().getSentimentForOneCase());
					if(comment.getResult().getSentimentForOneCase()>0){
						result1 = "p";
						sysPos ++;
					}
					else if(comment.getResult().getSentimentForOneCase()<0){
						result1 = "n";
						sysNeg ++;
					}
					else{
						result1 = "pn";
						sysPN ++;
					}
				}
				else{
					ArrayList<SentimentModel> sentiList = comment.getResult().getSentimentForMultCase();
					numberOfMultiEnityComment ++;
					result = "";
					result1 = "";
					for(int i=0; i<sentiList.size(); i++){
						if(i ==0 ){
							result = "";
						}
						result += sentiList.get(i).getWordSentiment()+"-"+Float.toString(sentiList.get(i).getScore())+" ";
						String sentiment = "";
						if(sentiList.get(i).getScore()>0){
							sentiment = "p";
						}
						else if(sentiList.get(i).getScore()<0){
							sentiment = "n";
						}
						else{
							sentiment = "pn";
		
						}
						
						result1 +=sentiList.get(i).getWordSentiment()+"-"+sentiment+" ";
					}			
				}
				
				
				out.write(result+"\n");
				out1.write(result1+"\n");
				String line1 = br1.readLine().replaceAll(" ", "");
				if(line1.compareTo("p") == 0){
					realPos++;
				}
				else if(line1.compareTo("n") == 0){
					realNeg ++;
				}
				else{
					realPN ++;
				}

				if(result1.replaceAll(" ", "").compareTo(line1)!=0){
					out2.write(Integer.toString(lineNumber)+"   "+line1+"   "+result+"\n");
					if(line1.compareTo("p") == 0){
						out3.write(Integer.toString(lineNumber)+"   "+line1+"   "+result+"     "+line+"\n");
					}
					else if(line1.compareTo("n") == 0){
						out4.write(Integer.toString(lineNumber)+"   "+line1+"   "+result+"     "+line+"\n");
					}
					else{
						out5.write(Integer.toString(lineNumber)+"   "+line1+"   "+result+"     "+line+"\n");
					}
				}
				
				if(!result1.equals("p")&&!result1.equals("n")&&!result1.equals("pn")){
					numberOfMultiEntityMistakeComments++;
					out6.write(lineNumber +"   " +line1 + "   "+result +"   "+"\n"+line +"\n");
				}
			}
			out6.write("-----------------------------------"+"\n");
			out6.write(Integer.toString(numberOfMultiEntityMistakeComments));
			out2.write("\n"+"***SUMMARY***"+"\n");
			out2.write("Reuslt by human:"+"\n");
			out2.write("realPos = "+realPos+"\n");
			out2.write("realNeg = "+realNeg+"\n");
			out2.write("realPN = "+realPN+"\n");
			out2.write("Reuslt of system:"+"\n");
			out2.write("sysPos = "+sysPos+"\n");
			out2.write("sysNeg = "+sysNeg+"\n");
			out2.write("sysPN = "+sysPN+"\n");
			out2.write("numberOfMultComments =: "+numberOfMultiEnityComment);
			fr.close();
			out1.close();
			out.close();
			out2.close();
			out3.close();
			out4.close();
			out5.close();
			out6.close();
			br1.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return System.currentTimeMillis()-time;
	}
	public static void  main(String []args){
		new Gui();
	}
}